package com.example.yangyuhua.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.yangyuhua.R
import com.example.yangyuhua.model.Student

class StudentAdapter(private val studentList: List<Student>) :
    RecyclerView.Adapter<StudentAdapter.StudentViewHolder>() {

    private fun getColorByStudentId(studentId: String): Int {
        return when (studentId.last().toString().toInt()) {
            in 0..1 -> android.graphics.Color.RED
            in 2..3 -> android.graphics.Color.GREEN
            in 4..5 -> android.graphics.Color.BLUE
            in 6..7 -> android.graphics.Color.CYAN
            in 8..9 -> android.graphics.Color.MAGENTA
            else -> android.graphics.Color.BLACK
        }
    }

    class StudentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textViewId: TextView = itemView.findViewById(R.id.textViewId)
        val textViewName: TextView = itemView.findViewById(R.id.textViewName)
        val textViewGender: TextView = itemView.findViewById(R.id.textViewGender)
        val textViewClass: TextView = itemView.findViewById(R.id.textViewClass)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StudentViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_student, parent, false)
        return StudentViewHolder(view)
    }

    override fun onBindViewHolder(holder: StudentViewHolder, position: Int) {
        val student = studentList[position]
        val color = getColorByStudentId(student.studentId)
        holder.textViewId.setTextColor(color)
        holder.textViewName.setTextColor(color)
        holder.textViewGender.setTextColor(color)
        holder.textViewClass.setTextColor(color)
        holder.textViewId.text = student.studentId
        holder.textViewName.text = student.name
        holder.textViewGender.text = student.gender
        holder.textViewClass.text = student.className
    }

    override fun getItemCount(): Int = studentList.size
}